package com.peng.lucene.searchtest;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.junit.jupiter.api.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * SearchTest1
 *
 * @author: lupeng10
 * @create: 2023-05-04 20:08
 */
public class SearchTest {

    @Test
    public void t1() throws ParseException, IOException {
        QueryParser parser = new QueryParser("brandName", new IKAnalyzer());
        Query query = parser.parse("name:华为手机");
        FSDirectory directory = FSDirectory.open(Paths.get("/tmp/lucene/data"));
        DirectoryReader directoryReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
        TopDocs topDocs = indexSearcher.search(query, 50);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            int docID = scoreDoc.doc;
            Document doc = indexSearcher.doc(docID);
            System.out.println("=============================");
            System.out.println("docID: " + docID);
            System.out.println("id: " + doc.get("id"));
            System.out.println("name: " + doc.get("name"));
            System.out.println("price: " + doc.get("price"));
            System.out.println("brandName: " + doc.get("brandName"));
            System.out.println("image: " + doc.get("image"));
        }
        directoryReader.close();
    }

    @Test
    public void rangeSearchTest() throws IOException {
        Query query = IntPoint.newRangeQuery("price", 100, 1000);
        FSDirectory directory = FSDirectory.open(Paths.get("/tmp/lucene/data"));
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        TopDocs topDocs = indexSearcher.search(query, 50);
        System.out.println("查询到的数据总条数是：" + topDocs.totalHits);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            int docID = scoreDoc.doc;
            Document doc = indexSearcher.doc(docID);
            System.out.println("=============================");
            System.out.println("docID: " + docID);
            System.out.println("id: " + doc.get("id"));
            System.out.println("name: " + doc.get("name"));
            System.out.println("price: " + doc.get("price"));
            System.out.println("brandName: " + doc.get("brandName"));
            System.out.println("image: " + doc.get("image"));
        }
        indexReader.close();
    }

    @Test
    public void boolQuery1Test() throws IOException {
        try (DirectoryReader directoryReader = DirectoryReader.open(FSDirectory.open(Paths.get("/tmp/lucene/data")))) {
            IndexSearcher indexSearcher = new IndexSearcher(directoryReader);

            IKAnalyzer analyzer = new IKAnalyzer();
            QueryParser parser = new QueryParser("name", analyzer);

            BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
            queryBuilder.add(new BooleanClause(parser.parse("华为手机"), BooleanClause.Occur.MUST));
            queryBuilder.add(new BooleanClause(IntPoint.newRangeQuery("price", 100, 1000), BooleanClause.Occur.MUST_NOT));

            TopDocs topDocs = indexSearcher.search(queryBuilder.build(), 50);
            System.out.println("查询到的数据总条数是: " + topDocs.totalHits);
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                int docID = scoreDoc.doc;
                Document doc = indexSearcher.doc(docID);
                System.out.println("=============================");
                System.out.println("docID: " + docID);
                System.out.println("id: " + doc.get("id"));
                System.out.println("name: " + doc.get("name"));
                System.out.println("price: " + doc.get("price"));
                System.out.println("brandName: " + doc.get("brandName"));
                System.out.println("image: " + doc.get("image"));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}
